package com.xiaohao.server.parkmap.controller;

import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaohao.server.parkmap.entity.*;
import com.xiaohao.server.parkmap.service.CustomerRewardService;
import com.xiaohao.server.parkmap.service.CustomerService;
import com.xiaohao.server.parkmap.service.ShopItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ShopItemService shopItemService;
    @Autowired
    private CustomerRewardService customerRewardService;

    @GetMapping("{wxOpenId}")
    public ResponseResult<Customer> query(@PathVariable String wxOpenId) {
        Customer customer = new Customer().setWxOpenId(wxOpenId);
        return ResponseResult.success(customerService.queryOne(customer));
    }

    @PostMapping("reward")
    public ResponseResult<?> reward(String wxOpenId, Integer shopItemId) {
        ShopItem shopItem = shopItemService.queryById(shopItemId);
        Customer customer = customerService.queryOne(new Customer().setWxOpenId(wxOpenId));
        if (customer.getReward() < shopItem.getReward()) {
            return ResponseResult.error("积分不足");
        } else {
            customer.setReward(customer.getReward() - shopItem.getReward());
            save(customer);
            CustomerReward customerReward =
                new CustomerReward().setCustomerId(customer.getId()).setShopItemId(shopItemId);
            customerRewardService.save(customerReward);
            return ResponseResult.success(customer);
        }
    }

    @GetMapping("reward/{wxOpenId}")
    public ResponseResult<List<ShopItem>> rewardMine(@PathVariable String wxOpenId) {
        Customer customer = customerService.queryOne(new Customer().setWxOpenId(wxOpenId));
        CustomerReward customerReward = new CustomerReward().setCustomerId(customer.getId());
        List<CustomerReward> customerRewardList = customerRewardService.query(customerReward);
        List<ShopItem> shopItems = new ArrayList<>();
        for (CustomerReward reward : customerRewardList) {
            ShopItem shopItem = shopItemService.queryById(reward.getShopItemId());
            shopItems.add(shopItem);
        }
        return ResponseResult.success(shopItems);
    }



    @GetMapping
    public ResponseResult<List<Customer>> query(Customer customer) {
        return ResponseResult.success(customerService.query(customer));
    }


    @GetMapping("total")
    public ResponseResult<?> totalCustomer() throws JsonProcessingException {
        final String url="http://q3596p3882.wicp.vip:10984/JadsPark/home/GetNewParkPeopleNum";
        String result= HttpUtil.get(url);
        ObjectMapper objectMapper = new ObjectMapper();
        TotalCustomer totalCustomer = objectMapper.readValue(result, TotalCustomer.class);
        return  ResponseResult.success(totalCustomer);
    }



    @DeleteMapping("{id}")
    public ResponseResult<Void> delete(@PathVariable Integer id) {
        return customerService.delete(id) > 0 ? ResponseResult.success() : ResponseResult.error();
    }

    @PostMapping
    public ResponseResult<Void> save(@RequestBody Customer customer) {
        return customerService.save(customer) > 0 ? ResponseResult.success() : ResponseResult.error();
    }


}
